package com.example.day8_shareprefrences_sdcard;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class LoginActivity extends AppCompatActivity {
    private EditText et_name,et_password;
    private Button bt_login;
    private CheckBox checkBox;
   // private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        //sharedPreferences = getSharedPreferences("login",MODE_PRIVATE);
        //TODO :读取数据并展示在页面
//        String name = sharedPreferences.getString("name", "");
//        String password = sharedPreferences.getString("password", "");
        String name = SPUtils.getInstance(this).getString("name");
        String password = SPUtils.getInstance(this).getString("password");
        et_name.setText(name);
        et_password.setText(password);
        if(!name.equals("")){
            checkBox.setChecked(true);
        }


    }

    private void initView() {
        et_name = findViewById(R.id.name);
        et_password = findViewById(R.id.passwod);
        bt_login = findViewById(R.id.bt_login);
        checkBox = findViewById(R.id.cb);
        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
    }
    //登陆
    private void login() {
        //TODO :判断是否选中
        if(checkBox.isChecked()){
            String name = et_name.getText().toString();
            String password = et_password.getText().toString();
//            SharedPreferences.Editor edit = sharedPreferences.edit();
//            edit.putString("name",name);
//            edit.putString("password",password);
//            edit.commit();
            SPUtils.getInstance(this).putString("name",name);
            SPUtils.getInstance(this).putString("password",password);

        }else{
//            SharedPreferences.Editor edit = sharedPreferences.edit();
//            edit.putString("name","");
//            edit.putString("password","");
//            edit.commit();
            SPUtils.getInstance(this).putString("name","");
            SPUtils.getInstance(this).putString("password","");
        }

    }
}
